package cn.e4j.modular.system.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.modular.system.entity.SysCustoms;
import cn.e4j.modular.system.mapper.SysCustomsMapper;
import cn.e4j.modular.system.service.SysCustomsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wenchuan.li
 * @since 2018-12-13
 */
@Service
public class SysCustomsServiceImpl extends ServiceImpl<SysCustomsMapper, SysCustoms> implements SysCustomsService {

    @Autowired
    SysCustomsMapper sysCustomsMapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCustomslist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> customsDtos = this.baseMapper.listCustomsPage(page, dto);

        return customsDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCustomsEdit(Dto dto) {

        List<Map<String, Object>> customsDtos = this.baseMapper.listCustomsEdit(dto);

        return customsDtos;
    }

    /**
     * 增加
     * @param dto
     * @return
     */
    @Transactional
    public Dto add(Dto dto){
        Dto outDto = Dtos.newDto();
        List<SysCustoms> list = new ArrayList<SysCustoms>();
        list = JSONObject.parseArray(dto.getString("mx_list"), SysCustoms.class);
        int count = 0;
        LocalDateTime updateTime = LocalDateTime.now();
        for(SysCustoms customs:list){
            customs.setCUSTOM_OBJ(dto.getString("CUSTOM_OBJ"));
            customs.setCUSTOM_OBJ_NAME_CN(dto.getString("CUSTOM_OBJ_NAME_CN"));
            customs.setCUSTOM_OBJ_NAME_TW(dto.getString("CUSTOM_OBJ_NAME_TW"));
            customs.setCUSTOM_OBJ_NAME_EN(dto.getString("CUSTOM_OBJ_NAME_EN"));
            customs.setCUSTOM_OBJ_NAME_CN(dto.getString("CUSTOM_OBJ_NAME_CN"));
            customs.setCUSTOM_REMARK(dto.getString("CUSTOM_REMARK"));
            // 加入统一的 创建时间 不然页面显示多条 wenchuan.li 20190723
            customs.setCREATE_DATE(updateTime);
            customs.insert();
            count++;
        }
        outDto.put("count",count);
        return outDto;
    }

    /**
     * 修改
     * @param dto
     * @return
     */
    @Transactional
    public Dto edit(Dto dto){
        Dto outDto = Dtos.newDto();
        sysCustomsMapper.delByCustomsObj(dto.getString("CUSTOM_OBJ"));
        List<SysCustoms> list = new ArrayList<SysCustoms>();
        list = JSONObject.parseArray(dto.getString("mx_list"), SysCustoms.class);
        int count = 0;
        LocalDateTime updateTime = LocalDateTime.now();
        for(SysCustoms customs:list){
            customs.setCUSTOM_OBJ(dto.getString("CUSTOM_OBJ"));
            customs.setCUSTOM_OBJ_NAME_CN(dto.getString("CUSTOM_OBJ_NAME_CN"));
            customs.setCUSTOM_OBJ_NAME_TW(dto.getString("CUSTOM_OBJ_NAME_TW"));
            customs.setCUSTOM_OBJ_NAME_EN(dto.getString("CUSTOM_OBJ_NAME_EN"));
            customs.setCUSTOM_OBJ_NAME_CN(dto.getString("CUSTOM_OBJ_NAME_CN"));
            customs.setCUSTOM_REMARK(dto.getString("CUSTOM_REMARK"));
            // 加入统一的 创建时间 不然页面显示多条 wenchuan.li 20190723
            customs.setCREATE_DATE(updateTime);
            customs.insert();
            count++;
        }
        outDto.put("count",count);
        return outDto;
    }
}
